﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web;
using System.Data.OracleClient;

namespace Antu.Business
{
    public class Attachment
    {
        public string FileTitle; //文件标题
        public string FileName;  //文件名
        public string ExtendName; //扩展名
        public System.IO.Stream FileStream;


        public Attachment()
        { }

        public Attachment(HtmlInputFile postFile)
        {
            if (postFile.PostedFile.ContentLength > 0)
            {
                //取得文件名（不包括路径）
                string strFileName = postFile.PostedFile.FileName;
                strFileName = strFileName.Substring(strFileName.LastIndexOf('\\'));

                FileName = strFileName; //文件名

                System.IO.FileInfo fl = new System.IO.FileInfo(FileName);
                ExtendName = fl.Extension; //扩展名

                //取得文件流
                FileStream = postFile.PostedFile.InputStream;
            }
        }
        /// <summary>
        /// 上传图片到文件
        /// </summary>
        /// <param name="FilePost">上传文件内容</param>
        /// <param name="FilePath">文件路径</param>
        /// <returns></returns>
        public static bool UpFileLoad(HtmlInputFile FilePost, string FilePath)
        {
            string strMsg = string.Empty;
            bool blReturn = true;

            if (System.IO.File.Exists(FilePath) == true)
            {
                System.IO.File.Delete(FilePath);
            }
            try
            {
                FilePost.PostedFile.SaveAs(FilePath);
                blReturn = true;
            }
            catch (Exception e)
            {
                strMsg = e.Message.ToString();
                blReturn = false;
            }

            return blReturn;
        }

        #region //返回菜单的图片路径
        /// <summary>
        /// 返回上传图片的文件名
        /// </summary>
        /// <param name="FilePost"></param>
        /// <param name="fileName">新的文件名</param>
        public static bool GetNewFileName(HtmlInputFile FilePost, ref string fileName,ref string Msg)
        {
            string FileName = "";
            string FileExpand = "";
            string StrFormat = ".JPG|.BMP|.GIF";  //允许上传图片的格式
            //string Msg = "";

            if (FilePost.PostedFile != null && FilePost.Value != "") //有上传信息
            {
                FileName = FilePost.PostedFile.FileName; //图片的名字
                FileInfo of = new FileInfo(FileName);
                FileExpand = of.Extension.ToUpper();
                if (StrFormat.IndexOf(FileExpand) < 0)
                {
                    Msg = "上传图片格式不正确,图片格式(GIF,JPG,BMP)";
                    return false;
                }
                if (FilePost.PostedFile.ContentLength > 2048)
                {
                    Msg = "上传图片太大（请上传小于2K的图片）";
                    return false;
                }

                string guid = Guid.NewGuid().ToString().Replace("-", "").Trim();
                fileName = guid + FileExpand;
                return true;
            }
            else
            {
                fileName = "";
                return true;
            }
        }
        #endregion

        /// <summary>
        /// 用户签名图片
        /// </summary>
        /// <param name="strUsername">用户名</param>
        /// <param name="strMsg"></param>
        /// <param name="isZip">是否压缩上传</param>
        /// <param name="type">type=1新增，type=2更新</param>
        /// <returns></returns>
        public bool SaveToDatabase(string strUsername, out string strMsg, bool isZip, int type)
        {
            bool bReturn = false;
            strMsg = string.Empty;
            string strSql = "";
            try
            {
                if (string.IsNullOrEmpty(FileName))
                {
                    strMsg = "文件路径有误";
                    return false;
                }

                if (FileStream == null)
                {
                    strMsg = "没选择上传文件";
                    return false;  //如果文件流对象为空则退出
                }

                Byte[] buffer = AntuZip.IO.Read2Buffer(FileStream, 0); //读取上传文件流

                FileStream.Close();

                if (isZip)  //要压缩文件
                {
                    AntuZip.Util.GetZipStream(ref buffer); //压缩
                }


                //以下代码是保存到数据库
                if (type == 1)
                {
                    //strSql = "insert into st_user(RESERVED1) values (:RESERVED1) where username='" + strUsername + "'";
                    strSql = "update st_user set RESERVED1=:RESERVED1 where username='" + strUsername + "'";
                }
                else
                {
                    strSql = "update st_user set RESERVED1=:RESERVED1 where username='" + strUsername + "'";
                }


                OracleConnection conn = new OracleConnection(SysParams.ConnectionString);
                conn.Open();
                OracleCommand command = new OracleCommand(strSql, conn);

                OracleParameter paramData = new OracleParameter("RESERVED1", OracleType.LongRaw, buffer.Length);
                paramData.Value = buffer;
                command.Parameters.Add(paramData);
                command.ExecuteNonQuery();

                conn.Close();

                bReturn = true;

            }
            catch (Exception ex)
            {
                strMsg = ex.Message.ToString();
            }

            return bReturn;
        }
    }
}
